// 返回按钮功能
function goBack() {
    window.history.back();
}

// 返回主界面
function goBackToMain() {
    window.location.href = 'index.html';
}

// 返回卡牌背包
function goBackToDeckBag() {
    window.location.href = 'deck-bag.html';
}

// 获取当前金钱
function fetchMoney() {
    fetch('/api/game/money')
        .then(response => response.json())
        .then(data => document.getElementById('currentMoney').textContent = data.money)
        .catch(error => console.error('获取金钱失败:', error));
}

// 创建主要角色卡
function createRoleElement(character) {
    // console.log(character);
    const card = document.createElement('div');
    card.className = `card`;

    card.innerHTML = `
        <img class="card-image" src="${character.imageUrl || 'https://via.placeholder.com/280x200?text=角色立绘'}" alt="${character.name}">
        <div class="card-content">
            <div class="card-name">${character.name}</div>
            <div class="card-description">${character.introduction}</div>
            <div class="card-stats">
                <div class="stat">
                    <span>生命值：</span>
                    <span>${character.hp}</span>
                </div>
                <div class="stat">
                    <span>法力值：</span>
                    <span>${character.mp}</span>
                </div>
            </div>
            <!-- 技能信息弹窗 -->
            <div class="skill-info">
                <h3>技能信息</h3>
                <div class="skill-details">
                    <div class="skill-name">${character.skill?.name || '暂无技能'}</div>
                    <div class="skill-introduction">${character.skill?.introduction || '暂无技能'}</div>
                    <div class="skill-effects">
                        ${character.skill ? `
                            ${character.skill.addSelfHp ? `<div>回血：+${character.skill.addSelfHp}</div>` : ''}
                            ${character.skill.addSelfMp ? `<div>回蓝：+${character.skill.addSelfMp}</div>` : ''}
                            ${character.skill.addSelfMpMax ? `<div>增加法力上限：+${character.skill.addSelfMpMax}</div>` : ''}
                            ${character.skill.addEnemyHp ? `<div>对敌方伤害：${character.skill.addEnemyHp}</div>` : ''}
                            ${character.skill.addEnemyMp ? `<div>减少敌方蓝量：${character.skill.addEnemyMp}</div>` : ''}
                            ${character.skill.cost ? `<div>技能消耗：${character.skill.cost} 法力值</div>` : ''}
                        ` : ''}
                    </div>
                </div>
            </div>
        </div>
    `;
    console.log(card);
    return card;
}

// 创建卡牌元素
function createCardElement(card) {
    const cardDiv = document.createElement('div');
    cardDiv.className = `card`;

    // 根据卡牌类型生成不同的属性显示
    let statsHTML = '';
    switch(card.type) {
        case 'role':
            statsHTML = `
                <div class="stat"><span>生命值：</span><span>${card.hp}</span></div>
                <div class="stat"><span>攻击力：</span><span>${card.attack}</span></div>
            `;
            break;
        case 'action':
            statsHTML = `
                ${card.addSelfHP ? `<div class="stat"><span>回血：</span><span>+${card.addSelfHP}</span></div>` : ''}
                ${card.addSelfMP ? `<div class="stat"><span>回蓝：</span><span>+${card.addSelfMP}</span></div>` : ''}
                ${card.addSelfMPMAX ? `<div class="stat"><span>增加法力上限：</span><span>+${card.addSelfMPMAX}</span></div>` : ''}
                ${card.addEnemyHP ? `<div class="stat"><span>对敌方伤害：</span><span>${card.addEnemyHP}</span></div>` : ''}
                ${card.addEnemyMP ? `<div class="stat"><span>减少敌方蓝量：</span><span>${card.addEnemyMP}</span></div>` : ''}
                ${card.cost ? `<div class="stat"><span>技能消耗：</span><span>${card.cost}</span></div>` : ''}
            `;
            break;
        case 'total_weapon':
            statsHTML = `
                ${card.addHPMax ? `<div class="stat"><span>增加生命上限：</span><span>+${card.addHPMax}</span></div>` : ''}
            `;
            break;
        case 'role_weapon':
            statsHTML = `
                ${card.addHPMax ? `<div class="stat"><span>增加生命上限：</span><span>+${card.addHPMax}</span></div>` : ''}
                ${card.addAttack ? `<div class="stat"><span>增加攻击力：</span><span>+${card.addAttack}</span></div>` : ''}
            `;
            break;
    }

    cardDiv.innerHTML = `
        <img class="card-image" src="${card.imageUrl || 'https://via.placeholder.com/240x160?text=卡牌立绘'}" alt="${card.name}">
        <div class="card-cost">${card.cost}</div>
        <div class="card-number">${card.count}</div>
        <div class="card-content">
            <div class="card-name">${card.name}</div>
            <div class="card-stats">
                ${statsHTML}
            </div>
        </div>
    `;

    return cardDiv;
}